package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.platform.entity.Organ;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 平台组织机构
 */
@Repository
public class OrganDao  extends BaseDao<Organ> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 保存或者更新
     * @return
     */
    public void saveOrUpdate(Organ organ){
        if(organ.getId()!=null && organ.getId()>0){
            this.update(organ);
        }else{
           this.add(organ);
        }
    }
    //详情
    public Organ get(Integer id){
        if(StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select o.*,(select po.title from bt_organ po where id = o.pid ) pname from bt_organ o where 1=1");
        sbStr.append(" and o.id = ? ");
        sbStr.append(" ORDER BY sorts DESC ");
        paramList.add(id);

        String sql = sbStr.toString();
        return this.queryObj(sql,paramList,Organ.class);
    }

    //列表查询
    public List<Organ> findList(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select o.*,(select po.title from bt_organ po where id = o.pid ) pname from bt_organ o where 1=1");
        if(!StringEx.isNullOrLing(paraMap.get("pid"))){
            sbStr.append(" and o.pid = ? ");
            paramList.add(paraMap.get("pid"));
        }
        sbStr.append(" ORDER BY sorts DESC ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,Organ.class);
    }

    /**
     * 获取所有系统中所有的结构列表信息
     */
    public List<Organ> getOrgAllList(){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_organ o ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,Organ.class);
    }

    /**
     * 删除
     */
    public void del(int id){
        if (id <= 0) {
            return;
        }
        String sql = "delete  from bt_organ  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }
}
